﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;

namespace Tesla.Graphics.Implementation {
    /// <summary>
    /// Abstract class for concrete subclasses to provide the underlying implementation of <see cref="RasterizerState"/>.
    /// </summary>
    public abstract class RasterizerStateImplementation : GraphicsResourceImplementation {
        private bool _isBound;
        private CullMode _cullMode;
        private VertexWinding _vertexWinding;
        private FillMode _fillMode;
        private int _depthBias;
        private float _slopeScaledDepthBias;
        private bool _scissorTestEnable;
        private bool _multiSampleEnable;

        /// <summary>
        /// Gets or protected sets if this BlendStateImplementation has been bound to the pipeline and its underlying objects created.
        /// </summary>
        public bool IsBound {
            get {
                return _isBound;
            }
            protected set {
                _isBound = value;
            }
        }

        /// <summary>
        /// Get or set how primitives are to be culled.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public CullMode Cull {
            get {
                return _cullMode;
            }
            set {
                ThrowIfBound();
                _cullMode = value;
            }
        }

        /// <summary>
        /// Get or set the vertex winding of a primitive, specifying the front face of the triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public VertexWinding VertexWinding {
            get {
                return _vertexWinding;
            }
            set {
                ThrowIfBound();
                _vertexWinding = value;
            }
        }

        /// <summary>
        /// Get or set the fill mode of a primitive.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public FillMode Fill {
            get {
                return _fillMode;
            }
            set {
                ThrowIfBound();
                _fillMode = value;
            }
        }

        /// <summary>
        /// Get or set the depth bias.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public int DepthBias {
            get {
                return _depthBias;
            }
            set {
                ThrowIfBound();
                _depthBias = value;
            }
        }

        /// <summary>
        /// Get or set the slope scaled depth bias.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public float SlopeScaledDepthBias {
            get {
                return _slopeScaledDepthBias;
            }
            set {
                ThrowIfBound();
                _slopeScaledDepthBias = value;
            }
        }

        /// <summary>
        /// Get or set if multisample anti-aliasing should be enabled.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public bool EnableMultiSampleAntiAlias {
            get {
                return _multiSampleEnable;
            }
            set {
                ThrowIfBound();
                _multiSampleEnable = value;
            }
        }

        /// <summary>
        /// Get or set if scissor testing should be enabled.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public bool EnableScissorTest {
            get {
                return _scissorTestEnable;
            }
            set {
                ThrowIfBound();
                _scissorTestEnable = value;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="RasterizerStateImplementation"/>.
        /// </summary>
        protected RasterizerStateImplementation() {
            _isBound = false;
        }

        /// <summary>
        /// Binds the implementation. This is called the first time an unbound state is set to the device or manually by the user in order to
        /// create the underlying state ahead of time (best practice). Once called the state properties are read-only.
        /// </summary>
        public abstract void BindRasterizerState();

        private void ThrowIfBound() {
            if(_isBound) {
                throw new InvalidOperationException("Cannot change render state once it has been bound to the pipeline.");
            }
        }
    }
}
